package com.sirdc.modules.sys.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.databean.SysUserDataBean;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.filter.SysDeptFilter;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping({"/modules/newSys/dept"})
public class DeptController extends JsonBaseController {

    @Autowired
    private SysDeptService sysDeptService;

    @Override
    protected String getView(String view) {
        return "/modules/sys/dept/" + view;
    }

    /**
     * 排序按钮
     *
     * @return url
     */

    @RequestMapping(value = "/sort", method = RequestMethod.GET)
    public String sortPage() {
        return getView("sort");
    }


    /**
     * 需要排序的数据
     *
     * @param filter
     * @return json
     */
    @ResponseBody
    @RequestMapping(value = "/sortData", method = RequestMethod.POST)
    public List<SysDept> sortData(@RequestBody SysDeptFilter filter) {
        List<SysDept> sysDepts = this.sysDeptService.query(filter);
        return sysDepts;
    }

    @ResponseBody
    @RequestMapping(value = "/sortDataUpdate", method = RequestMethod.POST)
    public Message sortDataUpdate(@RequestBody List<SysDept> list) {
        List<SysDept> deptList = sysDeptService.query();
        List<SysDept> updateList = new ArrayList<>();
        for (SysDept bean : list) {
            for (SysDept dept : deptList) {
                if(bean.getSysId().equals(dept.getSysId())){
                    dept.setOrderBy(bean.getOrderBy());
                    updateList.add(dept);
                    break;
                }
            }
        }
        sysDeptService.batchUpdate(updateList);
        return coverMessage("200", "排序更新成功!");
    }

}
